﻿using System.Security.Cryptography;
using System.Text;

namespace net8.Helper
{
    public class SecurityHelper
    {
        public static string Md5(string value)
        {
            var result = string.Empty;
            if (string.IsNullOrEmpty(value)) return result;
            using (var md5 = MD5.Create())
            {
                result = GetMd5Hash(md5, value);
            }
            return result;
        }

        static string GetMd5Hash(MD5 md5Hash, string input)
        {

            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            var sBuilder = new StringBuilder();
            foreach (byte t in data)
            {
                //加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
                sBuilder.Append(t.ToString("x2"));
            }
            return sBuilder.ToString();
        }

        public static string HmacSHA256(string msg)
        {
            byte[] keyByte = Encoding.GetEncoding("utf-8").GetBytes("123456789");
            byte[] messageBytes = Encoding.GetEncoding("utf-8").GetBytes(msg);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return Convert.ToBase64String(hashmessage);
            }
        }
    }
}
